package com.lagou.blog.controller;

import com.lagou.blog.entity.ArticleEntity;
import com.lagou.blog.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BlogController {

    @Autowired
    private ArticleRepository articleRepository;

    private static final Integer PAGE_SIZE = 3;

    @RequestMapping("/list")
    public String listArticles(HttpServletRequest httpServletRequest, Model model){
        String pageStr = httpServletRequest.getParameter("page");
        Integer page = 0;
        if (pageStr != null) {
            page = Integer.valueOf(pageStr);
        }
        Page<ArticleEntity> articleEntityList = articleRepository.findAll(PageRequest.of(page, PAGE_SIZE));
        model.addAttribute("articleList", articleEntityList);
        /*model.addAttribute("totalPage", articleEntityList.getTotalPages());
        model.addAttribute("isFirst", articleEntityList.isFirst());
        model.addAttribute("isLast", articleEntityList.isLast());*/
        return "client/index";
    }


}
